﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bzure.Common.Core;
using Bzure.Common.Data;

namespace Bzure.Common.Configuration.AppConfiguration
{
    public class ConfigTemplateModel : IConifgTemplateModel
    {
        public delegate void UpdateConfigTemplateHanlder(IConfigTemplate Template, string ConfigKey, string ConfigValue);

        /// <summary>
        /// 配置模板实体
        /// </summary>
        public IConfigTemplate Entity { get; private set; }
        public event UpdateConfigTemplateHanlder OnConfigTemplateUpdated;
        Dictionary<string, string> configs;
        public IEnumerable<KeyValuePair<string, string>> Configurations
        {
            get
            {
                return configs.AsEnumerable();
            }
        }
        public ConfigTemplateModel(string TemplateKey)
        {
            this.Entity = new ConfigTemplate()
            {
                TemplateKey = TemplateKey
            };
            this.configs = new Dictionary<string, string>();
        }


        public ConfigTemplateModel(IConfigTemplate entity)
        {
            this.Entity = entity;
            this.configs = new Dictionary<string, string>();
            BuildConfiguration();
        }
        /// <summary>
        /// 构造配置
        /// </summary>
        void BuildConfiguration()
        {
            foreach (KeyValuePair<string, string> pair in this.Entity.ToKeyValuePairs())
            {
                this.configs[pair.Key] = pair.Value;
            }
        }
        public void AppendConfig(string ConfigKey, string ConfigValue)
        {
            if (string.IsNullOrEmpty(ConfigKey))
                throw new Exception("配置键不能为空");
            this.Entity.Validation(ConfigKey);
            this.Entity.Validation(ConfigValue);
            this.configs[ConfigKey] = ConfigValue;
            this.Entity.FromKeys(this.configs.Keys);
            this.Entity.FromValues(this.configs.Values);
            Save();
            if (this.OnConfigTemplateUpdated != null)
                this.OnConfigTemplateUpdated(this.Entity, ConfigKey, ConfigValue);
        }
        public void Save()
        {
            EntityPostRequest request = new EntityPostRequest(EntityPostRequest.EntityPostMethod.Update)
            {
                Entity = this.Entity
            };
            request.AppendConditionMap(this.Entity.TemplateKey, () => this.Entity.TemplateKey);
            request.AppendFieldMap(this.Entity.Expression, () => this.Entity.Expression);
            request.AppendFieldMap(this.Entity.Value, () => this.Entity.Value);
            Actor.Public.BuildEntityProcessor(ConfigTemplateSource.Name).UpdateEntity(request);
        }
        /// <summary>
        /// 获取指定的配置模板
        /// </summary>
        /// <param name="TemplateKey"></param>
        /// <returns></returns>
        public static IConfigTemplate GetConfigTemplate(string TemplateKey)
        {
            ConfigTemplate factor = new ConfigTemplate()
            {
                TemplateKey = TemplateKey
            };
            EntityQueryRequest<ConfigTemplate> query = new EntityQueryRequest<ConfigTemplate>();
            query.AppendConditionMap(factor.TemplateKey, () => factor.TemplateKey);
            ConfigTemplate[] entities = Actor.Public.BuildEntityProcessor(ConfigTemplateSource.Name).QueryEntity(query);
            if (entities != default(ConfigTemplate[]) && entities.Length > 0)
                return entities[0];
            return default(IConfigTemplate);
        }
    }
}
